Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Mermaid pan & zoom #295

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open

Feature: Mermaid pan & zoom #295

wants to merge 30 commits into from

Conversation

snopan
Copy link

@snopan snopan commented Nov 13, 2024

This PR adds pan & zoom functionality in mermaid MD preview. There's a previous PR that attempted to add this functionality but had some issues. This PR tries address the issues mentioned in the comments.

#125

image

The changes include:

  • Rendering a button about the diagram when there are no errors in diagram
  • When svg-pan-zoom hasn't initiated, the behavior should be same as before
  • Only when the button is pressed, svg-pan-zoom is initiated allowing for pan and zoom. The sizing of diagram will be set to what it is at that point and it also starts recording any actions taken (pan/zoom) into ZoomStates
  • When the markdown is re-rendered, it will go check ZoomStates and sync back to where it is allowing for zoom and editing at the same time
  • Unrelated it also removes the throw error when a diagram fails to parse. From what I can see when mermaid library fails to parse the contents, we would catch the error and return a error message content. But for some reason it makes another throw error, so when you have multiple diagrams and one breaks it breaks everything else.
image

after remove throw error
image

@snopan
Copy link
Author

snopan commented Nov 13, 2024

Hey @mjbvz, can I get a review on this PR? Thanks!

Copy link
Owner

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this! Sorry it took me so long to test it

The panning and zooming seems to work well. However to start I think it should be presented differently. Let's do:

  • Add a new vscode configuration setting that lets you enable/disable pan zoom entirely. It can default to on, but there needs to be a way to turn the feature off entirely

  • Only show the pan/zoom controls when you hover over the diagram

  • Potentially add a command or right click option on the diagram that overrides the VS Code configuration. So if I have pan zoom enabled in my settings, I'd like the command to be able to turn it off in case it's causing any issues

A few other points I noticed:

  • The colors of the controls seem off, especially on dark themes. If you can, reuse some of the vscode theme colors. These should all be available inside of the markdown preview webview

  • The diagram get cut off. I'd try to get it match what GitHub does instead where the diagram is centered and takes up more horizontal space

I will take a more detailed look once some of those points are addressed. Let me know if you have any questions about these

@snopan
Copy link
Author

snopan commented Nov 26, 2024

Hey @mjbvz , thanks for getting back to me! I just slightly confused and want to clarify a few things:

  • Do we still want to keep the toggle button or is the pan/zoom now controlled by this vscode configuration?
  • So when you mention show pan/zoom controls on hover, do you mean the toggle button
image

or these controls after pan/zoom is enabled
image

  • When you say right click and turn off pan/zoom, does that also turn off pan/zoom entirely like the VS Configuration.
  • Can you give me a bit more details or steps taken to get to this "diagram get cut off" behaviour? I mostly just rely on svg-pan-zoom library to do the pan/zoom and have some extra logic to store the state of pan/zoom.

@mjbvz
Copy link
Owner

mjbvz commented Dec 2, 2024

Sure, following up:

  • The toggle feels too prominent to show on every single diagram. Let's start with using the configuration plus maybe command /right click for quick toggling just for the current preview

  • For the controls, I mean the +/-. I only care about these if I'm interacting with the diagram. Again it feels cluttered to show them all the time

  • Let's try to match how word wrap works in editors works. With word wrap, the setting provides the default value but you can then override this per editor using the command. The command however only applies to the current document, it does not change the configuration

  • Try creating various sized diagram and then zooming in / panning. You can also try making the markdown preview itself larger or smaller. I was fairly easily able to get the diagram to be cut off horizontally when doing this

@snopan
Copy link
Author

snopan commented Jan 6, 2025

Hey @mjbvz, sorry haven't been able to work on it much been kinda busy with other stuff.

I've made a few changes according to some of your comments

Toggle and Controls being too prominent

  • Added a vscode config field "enablePanZoom" which by default is true but when set to false will not render with any pan zoom functionality
  • Both toggle and controls will only appear when hovered on so they aren't there all the time
  • Given the above two changes, I didn't work on adding any commands or right click functionality, feels like the code would get a bit more messier and need to refactor a tad bit.

Color theme issues

  • For the toggle I've changed the styling to use vscode color variables, not sure if they're the exact correct ones. I just picked a background that is darker than the preview background then just a foreground and active color.
  • For the Controls that one is a bit more tricky, so that is created by svg-pan-zoom and a bit difficult to edit the styling. The way to get around it is to disable their controls and we create our own custom one, which is a bit more work. Not sure if we can put it as an uplift item later on.

Diagram being cut off

So for this issue I'm assuming you were talking about the diagram not taking the full width of the page when pan zoom is enabled. The reason for this was because I wanted the switching between pan zoom to look seamless, so when it gets turned on it takes the current size dimension of the diagram. But given it doesn't use the full width of the page would look like it's cut off.
image

I've updated so that it does take the full width of the page when pan zoom is enabled, but the height is still the height of the current diagram so when you resize the width down it does look a bit skinny and tall which is something that we have to accept I think because the diagram needs a set height when pan zoom is enabled.
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants